#!/bin/sh
. /etc/init.d/rc.functions

P(){
  printf " %s %s %s %s" $@
  `$@` 1>/dev/null 2>/dev/null
  status
}
USBInstall(){
  
  echo "============================================================================"
  printf "%s\n" "`blkid`"
  echo "============================================================================"
  printf "%s" " Please enter the target device (sda1,hda3...):"
  INPUT=""; read INPUT
  printf "\n\n"
  if blkid | grep "/dev/$INPUT:"; then
    TargetDev=$INPUT
  else
    printf "\n%s\n" " Invalid device.  ex: sda1, hda7..."
    exit 0
  fi
  printf "\n%s" " Format /dev/$TargetDev first? y/n: "
  INPUT=""; read -s -n1 INPUT
  case $INPUT in
    [Yy] ) printf "%s\n\n" "YES"; mkfs.ext2 -L "Squid4SliTaz" /dev/$TargetDev;;
       * ) echo "NO";;
  esac
  echo "exit
  " | tazusb gen-iso2usb Squid4SliTaz.iso /dev/$TargetDev
  P mkdir -p /mnt/$TargetDev
  P mount /dev/$TargetDev /mnt/$TargetDev
  if [ -f isolinux.cfg ]; then
    P cp isolinux.cfg /mnt/$1/boot/extlinux/isolinux.cfg
  fi
}

BurningQuestions(){
printf "%s" "Would you like to burn the ISO to a CDROM? y/n: "
INPUT=""; read -s -n1 INPUT
  case $INPUT in
    [Yy] ) printf "%s\n\n" "YES"; tazlito burn-iso squid4slitaz.iso; exit 0;;
       * ) printf "%s\n\n" "NO";;
  esac
printf "%s" "Would you like to write the ISO to a USB device? y/n: "
INPUT=""; read -s -n1 INPUT
  case $INPUT in
    [Yy] ) printf "%s\n\n" "YES"; USBInstall;;
       * ) printf "%s\n\n" "NO";;
  esac
}

Exit(){
  printf "\n%s\n%s\n%s\n%s\n\n" "============  Build Squid4SliTaz ISO Complete  =============================" "  NOTICE: ISO is generated using the running fs" "          All changes that have been made will be included" "          This may include personal or sensitive data!"
  exit 0
}
  
printf "\n%s\n%s\n%s\n%s\n\n" "============  Build Squid4SliTaz ISO  ======================================" "  NOTICE: ISO is generated using the running fs" "          All changes that have been made will be included" "          This may include personal or sensitive data!"

if [ $1 ]; then SlitazISO=$1; else SlitazISO="slitaz-4.0.iso"; fi
if [ ! -f slitaz*.iso ]; then
  echo " Downloading slitaz-4.0.iso.  Please wait..."
  P wget http://mirror.slitaz.org/iso/4.0/slitaz-4.0.iso
fi

if [ -f Squid4SliTaz.iso ]; then
  printf "%s" " Found Squid4SliTaz.iso, use this ISO? y/n: "
  INPUT=""; read -s -n1 INPUT
  case $INPUT in
    [Yy] ) printf "%s\n\n" "YES"; BurningQuestions; Exit;;
       * ) echo "NO";;
  esac     
fi       

printf "%s" " Remove sound card and screen configs? y/n: "
RemoveConfigs="no"
INPUT="";read -s -n1 INPUT
case $INPUT in
  [Yy] ) printf "%s\n" "YES"; RemoveConfigs="yes";;
     * ) printf "%s\n" "NO"; RemoveConfigs="no";;
esac



P tazpkg recharge
P tazpkg -gi bc
P tazpkg -gi iptables
P tazpkg -gi squid
P cp -rf copy2fs/* /

if [ ! -f /media/cdrom/boot/bzImage ]; then
  P umount /media/cdrom
  P mount -o loop $SlitazISO /media/cdrom
fi
if [ ! -f /media/cdrom/boot/vmlinuz-*-slitaz ]; then
  echo "============================================================================"
  printf "\n%s\n\n" " Failed to mount ISO to /media/cdrom..."
  exit 0
fi

printf "%s" " tazlito writiso lzma "
echo "$RemoveConfigs
exit
" | tazlito writeiso lzma 1>/dev/null 2>/dev/null
status
if [ ! -f /home/slitaz/distro/slitaz.iso ]; then
  echo "============================================================================"
  printf "\n%s\n\n" " Failed to build ISO.  Check that the slitaz ISO is mounted to /media/cdrom..."
  exit 0
fi
P cp /home/slitaz/distro/slitaz.iso squid4slitaz.iso
P cp /home/slitaz/distro/slitaz.md5 squid4slitaz.md5
P rm -rf /home/slitaz/distro
P umount /media/cdrom
P mount /media/cdrom


echo "============================================================================"
printf "\n%s\n%s\n%s\n\n" " buildiso.sh finished successfully!" " squid4slitaz.iso can now be burned to a CD using \`tazlito burn-iso squid4slitaz.iso\`" " It can also be written to a usb disk with \`tazusb gen-iso2usb squid4slitaz.iso\`"
BurningQuestions
Exit
